package com.glodon.paas.framework.registry.sample;

import com.glodon.paas.framework.registry.client.web.ClientBootstrapListener;
import com.glodon.paas.framework.registry.core.status.ClientStatus;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Don Li
 */
public class ClientStatusServlet extends HttpServlet {
    private ServletContext context;

    @Override
    public void init(ServletConfig config) throws ServletException {
        this.context = config.getServletContext();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String status = req.getParameter("status");
        if ("up".equals(status)) {
            context.setAttribute(ClientBootstrapListener.CLIENT_STATUS, ClientStatus.UP);
            resp.getWriter().write("Client is UP now!");
        } else if ("down".equals(status)) {
            context.setAttribute(ClientBootstrapListener.CLIENT_STATUS, ClientStatus.DOWN);
            resp.getWriter().write("Client is DOWN now!");
        } else if ("out".equals(status)) {
            context.setAttribute(ClientBootstrapListener.CLIENT_STATUS, ClientStatus.OUT_OF_SERVICE);
            resp.getWriter().write("Client is OUT_OF_SERVICE now!");
        } else if ("start".equals(status)) {
            context.setAttribute(ClientBootstrapListener.CLIENT_STATUS, ClientStatus.STARTING);
            resp.getWriter().write("Client is STARTING now!");
        }
    }
}
